package com.Servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.text.DecimalFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jdbc.Jdbc;

/**
 * Servlet implementation class worksales
 */
@WebServlet("/worksales")
public class worksales extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public worksales() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		String id = request.getParameter("id");
		//获取中心编号后，例如“SH001”,接着从流水号表中获取最新的流水号，并且更新流水号
		//获取流水号，并且修改新的流水号
		String select ="select * from serialnumber";
		String serial = null;
		Jdbc se = new Jdbc();
		try {
			se.Jdbc_Q2(select);
			se.rs.next();
			serial = se.rs.getString("serialnum");
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		int serialInt =Integer.parseInt(serial);//将String转换成INT类型
		DecimalFormat myformat = new DecimalFormat();//设置流水号编码格式
		myformat.applyPattern("000000");
		String res =(String)myformat.format(serialInt+1);//获取最新的流水号
		//并且要将最新的流水号写入流水号表格中
		String update="update serialnumber set serialnum='"+res+"'";
		try {
			Jdbc se1 = new Jdbc(update);
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
		
		
		
		//流水号部分结束
		String cname = request.getParameter("chinesename");
		String ename = request.getParameter("englishname");
		String sex = request.getParameter("sex");
		String birthday = request.getParameter("birthday");
		String reservaiton = "未预约";
		String attend = "未出席";
		String messagefrom = request.getParameter("messagefrom");
		String marketman = request.getParameter("marketman");
		String counselor = request.getParameter("counselor");
		String pcounselor = request.getParameter("phonecounselor");
		String disstate = request.getParameter("distributionstate");
		String distime = request.getParameter("distributiontime");
		String phone1  = request.getParameter("phone1");
		String phone2 = request.getParameter("phone2");
		String remarks = request.getParameter("remarks");
		String username = (String) request.getSession().getAttribute("username");
		
		
		
		
		String finalserial = id+res;
		//插入数据库部分
		//将所有获取的属性写入插入语句中去
		String insert = "insert into sales(id, chinesename,englishname,sex,birthday,reservation,attend,messagefrom,marketman,"
				+ "counselor,phonecounselor,distributionstate,distributiontime,phone1,phone2,remarks,username)values('"+finalserial+"','"+cname+"','"+ename+"','"+sex+"',"
						+ "'"+birthday+"','"+reservaiton+"','"+attend+"','"+messagefrom+"','"+marketman+"','"+counselor+"','"+pcounselor+"','"+disstate+"',"
								+ "'"+distime+"','"+phone1+"','"+phone2+"','"+remarks+"','"+username+"')";
		Jdbc j1 = null;
	
		try {
			j1= new Jdbc(insert);
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
			
			 request.getRequestDispatcher("showsuccess.jsp").forward(request, response);
	}

}
